1 00:00:00,750 --> 00:00:01,380 Hey there. 2 00:00:01,380 --> 00:00:02,310 Welcome back. 3 00:00:02,310 --> 00:00:06,030 Let's go ahead and add some footsteps to our player. 4 00:00:06,030 --> 00:00:09,930 This is going to be virtually the exact same as what we've done in our previous project. 5 00:00:09,930 --> 00:00:13,890 So you can actually just copy all of the code and put it in this project. 6 00:00:13,890 --> 00:00:18,450 But I'm still going to go through and script the whole system for creating footsteps. 7 00:00:19,160 --> 00:00:23,660 So we can go ahead and create a new local script inside of starter character scripts. 8 00:00:23,660 --> 00:00:27,890 And this is going to be our footsteps handler. 9 00:00:27,890 --> 00:00:33,020 And we're going to go ahead and use the module script inside of Replicated storage, which is our footstep 10 00:00:33,020 --> 00:00:39,740 module, to go ahead and get random sound IDs based on whatever material we are walking on. 11 00:00:40,760 --> 00:00:46,760 I'm going to be using the same template as before, but we're not going to be having this be a service, 12 00:00:46,760 --> 00:00:53,090 but instead we just want the nice little comment separations to keep our code nice and organized. 13 00:00:53,180 --> 00:00:58,160 And all we're going to need is replicated storage to get that module script. 14 00:00:59,000 --> 00:01:02,210 So first we'll make a reference to that module. 15 00:01:02,210 --> 00:01:04,040 I'll just call it footstep module. 16 00:01:04,040 --> 00:01:08,420 And we'll require from replicated storage dot modules dot footstep module. 17 00:01:08,690 --> 00:01:12,350 We'll get the players character which is literally just script dot parent. 18 00:01:12,350 --> 00:01:19,250 We will get the humanoid which is going to be equal to the character, and we'll wait for the humanoid 19 00:01:19,250 --> 00:01:19,850 instance. 20 00:01:19,850 --> 00:01:23,120 We'll also get the route part and that's character. 21 00:01:23,120 --> 00:01:26,510 And we'll wait for the humanoid route part as well, just in case. 22 00:01:27,370 --> 00:01:31,600 We're also going to get the default running sound or walking sound. 23 00:01:31,630 --> 00:01:33,070 We'll call it running sound. 24 00:01:33,070 --> 00:01:35,080 And that's in the root part. 25 00:01:35,440 --> 00:01:37,390 And it's called running. 26 00:01:37,940 --> 00:01:44,240 And we want to just basically update that value on the volume for this running sound equal to zero. 27 00:01:45,110 --> 00:01:48,920 Um, some other things we want to keep track of is the player's original walking speed. 28 00:01:48,920 --> 00:01:54,170 So we'll call this variable walk speed equal to humanoid walk speed. 29 00:01:55,720 --> 00:02:00,130 We'll get the player's current speed and a variable that'll be equal to zero for now. 30 00:02:00,890 --> 00:02:06,620 And then we can have another variable store, a value that's going to be our cooldown or the time between 31 00:02:06,620 --> 00:02:07,400 footsteps. 32 00:02:07,400 --> 00:02:13,400 By default we'll set it to 0.35, but if we detect that the player's walk speed increases or decreases, 33 00:02:13,400 --> 00:02:15,260 we'll update that value accordingly. 34 00:02:15,830 --> 00:02:21,830 And then once we do that, we can just literally set the running student volume equal to zero. 35 00:02:21,830 --> 00:02:25,340 And we'll actually do that inside of our main section here. 36 00:02:26,850 --> 00:02:30,450 But now that we've got all that updated, we can go ahead and create some functions we'll need. 37 00:02:30,450 --> 00:02:34,410 For example, we can have a function for playing a footstep sound. 38 00:02:36,580 --> 00:02:39,820 And we'll pass a sound ID to this function. 39 00:02:40,120 --> 00:02:44,020 We'll also want to have a function to check whether or not our player is moving. 40 00:02:44,020 --> 00:02:45,730 So we'll call it check movement. 41 00:02:48,430 --> 00:02:55,630 And then we'll need a function to get the, uh, sound ID based on whatever material we are standing 42 00:02:55,630 --> 00:02:56,350 on. 43 00:02:56,350 --> 00:03:02,890 So we can call this get sound based on material. 44 00:03:03,640 --> 00:03:08,530 And then what we could do is I'm going to rename this here to be our handler section, because we're 45 00:03:08,530 --> 00:03:10,630 going to be listening to some events. 46 00:03:10,630 --> 00:03:16,450 For example, we want to listen for when the property of our walk speed changes on our humanoid. 47 00:03:17,240 --> 00:03:24,260 And when it does, we want to go ahead and check to see if the humanoid dot walk speed is greater than 48 00:03:24,260 --> 00:03:25,700 the walk speed in our variable. 49 00:03:25,700 --> 00:03:29,570 And if it is, we want to update the cooldown to be less. 50 00:03:29,570 --> 00:03:32,450 I'll say something like 0.25. 51 00:03:32,900 --> 00:03:39,740 If the walk speed of our humanoid becomes less than the default recorded walk speed, then we're going 52 00:03:39,740 --> 00:03:41,840 to set cooldown to be longer. 53 00:03:43,030 --> 00:03:49,240 Otherwise, the default for our cool down again is going to be 0.35 seconds. 54 00:03:51,160 --> 00:03:55,720 Another thing we want to listen to is the running event for our humanoid. 55 00:03:59,000 --> 00:04:03,770 And this will pass to us the current speed of our player, and then we can just go ahead and set the 56 00:04:03,770 --> 00:04:06,860 current speed equal to that running speed. 57 00:04:07,840 --> 00:04:14,320 And now inside of the main section, we'll have a while loop that will constantly update the movement 58 00:04:14,320 --> 00:04:18,280 of our player, and if we're moving, then we'll go ahead and play a footstep sound. 59 00:04:18,310 --> 00:04:22,360 Now we're going to have to create a footstep sound inside of our player. 60 00:04:22,360 --> 00:04:26,260 So what I'm going to do is I'm going to create a new sound instance. 61 00:04:26,260 --> 00:04:28,090 I'm going to call it footstep sound. 62 00:04:28,120 --> 00:04:31,510 That's going to be equal to instance dot new sound. 63 00:04:32,140 --> 00:04:36,460 We're going to set the name of the sound equal to footsteps. 64 00:04:38,190 --> 00:04:43,320 We're going to set the volume to like 0.15. 65 00:04:43,800 --> 00:04:49,800 And then we'll update the roll off max distance equal to like 30 studs. 66 00:04:50,070 --> 00:04:57,090 The footstep sound roll off mode is going to be equal to enum dot roll off mode dot linear. 67 00:04:57,660 --> 00:04:59,940 Uh or maybe I think it's inverse. 68 00:04:59,940 --> 00:05:00,120 Yeah. 69 00:05:00,120 --> 00:05:02,100 We'll do inverse tapered. 70 00:05:02,940 --> 00:05:07,620 And then the parent of our footstep sound of course is going to be equal to our root part. 71 00:05:08,590 --> 00:05:13,540 Now we can go ahead and do is I'll use task dot defer to spawn a new function. 72 00:05:13,540 --> 00:05:16,270 And inside of here we'll have our while loop. 73 00:05:16,270 --> 00:05:20,080 And what this while loop is going to do is check whether or not our player is moving. 74 00:05:20,080 --> 00:05:24,790 So we'll have uh our check movement function return a boolean. 75 00:05:25,000 --> 00:05:30,430 So if we check our movement and it's true, then we can go ahead and play a footstep sound. 76 00:05:30,430 --> 00:05:34,600 And that's where we would call our play footstep sound function and pass the sound ID. 77 00:05:34,900 --> 00:05:39,610 And since we'll get the sound ID based on whatever material we're standing on, then we can literally 78 00:05:39,610 --> 00:05:48,190 call get sound based on material within the parentheses or the call of our play footsteps down. 79 00:05:48,190 --> 00:05:53,830 So this will play our footstep, and then we'll go ahead and yield inside of our loop for that. 80 00:05:53,830 --> 00:05:53,950 Uh. 81 00:05:53,950 --> 00:05:55,090 Cool down. 82 00:05:56,340 --> 00:06:00,090 And now all we need to do is go ahead and fill out these three functions here. 83 00:06:00,450 --> 00:06:06,420 So for example, to check the movement of the humanoid, first we want to go ahead and get the last 84 00:06:06,420 --> 00:06:09,090 material that our humanoid was standing on. 85 00:06:09,090 --> 00:06:13,830 So it's going to be equal to humanoid dot floor material. 86 00:06:14,900 --> 00:06:17,930 And actually I think we forgot to make a variable for this. 87 00:06:17,930 --> 00:06:20,240 So I'll just make it up here. 88 00:06:20,240 --> 00:06:23,630 We'll call this last material and set it equal to nil. 89 00:06:23,630 --> 00:06:28,250 So we'll get the last material that the humanoid was standing on. 90 00:06:28,730 --> 00:06:33,980 And we want to make sure that our humanoids walk speed is greater than zero. 91 00:06:33,980 --> 00:06:38,090 We also want to make sure the humanoid and we get their move direction. 92 00:06:38,090 --> 00:06:42,890 Actually, let me go ahead and denote the humanoid as a humanoid because it's not showing us that autofill 93 00:06:42,890 --> 00:06:43,550 stuff. 94 00:06:43,700 --> 00:06:47,300 So if the humanoid dot move direction. 95 00:06:48,510 --> 00:06:56,070 Dot magnitude is greater than zero, and we validate that our current speed is greater than zero. 96 00:06:56,610 --> 00:06:59,520 And we want to make sure that we're actually standing on something. 97 00:06:59,520 --> 00:07:03,300 So we want to make sure that last material is not equal to nil. 98 00:07:03,660 --> 00:07:07,950 Or we want to make sure that the last material we are standing on is not air. 99 00:07:07,950 --> 00:07:11,580 So we'll make sure that's not equal to the enum dot material of air. 100 00:07:12,300 --> 00:07:17,760 If all of this matches up, well, then we can go ahead and return true. 101 00:07:17,760 --> 00:07:20,160 Otherwise we'll just return false. 102 00:07:20,550 --> 00:07:26,760 So if we're moving, that means we'll grab a sound based on what material we're standing on. 103 00:07:26,760 --> 00:07:32,880 So that means inside of our footstep module there's a function called get table from material. 104 00:07:32,880 --> 00:07:36,990 So it'll return to us a table of sound IDs based on a material. 105 00:07:37,350 --> 00:07:41,280 And that's going to be equal to whatever last material we were standing on. 106 00:07:42,120 --> 00:07:45,420 So we'll create a variable called sound table. 107 00:07:46,760 --> 00:07:51,320 And then we can go ahead and get a random sound from the sound table. 108 00:07:51,710 --> 00:07:54,320 So we'll create a variable called random sound. 109 00:07:54,320 --> 00:07:59,960 And that's going to be equal to inside of the footstep module there's a function called get random sound. 110 00:07:59,960 --> 00:08:01,880 And we pass a table here. 111 00:08:01,880 --> 00:08:03,860 So that's going to be our sound table. 112 00:08:04,070 --> 00:08:07,340 And then we can go ahead and return this random sound. 113 00:08:08,370 --> 00:08:12,720 Then we pass this random sound to our play footstep sound function. 114 00:08:13,320 --> 00:08:19,890 So when we do that, we want to go ahead and do is we want to get the footstep sound in our player. 115 00:08:19,890 --> 00:08:24,600 So we'll create a variable called footstep sound and that's equal to root part. 116 00:08:24,600 --> 00:08:28,320 Find first child footsteps. 117 00:08:28,710 --> 00:08:36,540 If for some reason this does not exist, then we're just going to return. 118 00:08:36,540 --> 00:08:39,090 But I don't anticipate this not existing. 119 00:08:39,090 --> 00:08:40,680 But for some reason something could happen. 120 00:08:40,680 --> 00:08:42,120 You never know in the future. 121 00:08:42,120 --> 00:08:49,770 But once we do that, we can go ahead and update the footstep sound dot sound ID equal to the sound 122 00:08:49,800 --> 00:08:52,920 ID that was passed to this function. 123 00:08:53,600 --> 00:08:58,400 And then we can just play the footstep sound just like that. 124 00:08:59,530 --> 00:09:05,110 So if we go and playtest our game and we get that local script copied to our player's character if we 125 00:09:05,110 --> 00:09:06,370 start walking around. 126 00:09:10,130 --> 00:09:15,530 As you can see, we are playing footsteps based on whatever material we are standing on. 127 00:09:19,100 --> 00:09:22,250 And then if I go ahead and increase the speed of my player. 128 00:09:22,250 --> 00:09:28,370 So if I go and update in my player's character the walk speed, let's say I set the walk speed to like 129 00:09:28,370 --> 00:09:31,670 24 and then I go back to my client. 130 00:09:32,690 --> 00:09:38,300 As you can see, we updated that cooldown value and now it is playing the footsteps much faster. 131 00:09:42,520 --> 00:09:48,700 And if I go back and I update the value to be something lower, for example, maybe maybe we'll set 132 00:09:48,700 --> 00:09:50,710 it to like 12. 133 00:09:51,700 --> 00:09:52,960 And then we go back. 134 00:09:55,000 --> 00:09:58,060 As you can see, the footsteps are playing much slower now. 135 00:10:01,940 --> 00:10:06,050 And just like that, we have successfully added footsteps to our game. 136 00:10:06,050 --> 00:10:07,340 Not much to it.